from appium.webdriver.common.appiumby import AppiumBy

from src.entity.we_work_app import WeWorkApp
from src.page.main_page import MainPage


class ContactPage(MainPage):
    """
    通讯录界面类
    """
    _ADD_MEMBER_BTN = AppiumBy.XPATH, '//android.widget.TextView[@resource-id="com.tencent.wework:id/mid1Txt" and ' \
                                      '@text="添加成员"]'
    _CONTACT_MANAGE_BTN = AppiumBy.XPATH, '//android.widget.TextView[@resource-id="com.tencent.wework:id/mi1"]'

    def goto_add_member_page(self):
        """
        跳转添加联系人
        """
        print("点击【添加成员】按钮")
        self.driver.vertical_swipe_find_and_click(*self._ADD_MEMBER_BTN)

        # 跳转添加成员页面
        self.load_wait()
        return AddMemberPage(self.driver)

    def goto_contact_manage(self):
        """
        跳转联系人管理页面
        """
        print("点击右上角图标按钮")
        self.driver.find_and_click(*self._CONTACT_MANAGE_BTN)

        # 进入联系人管理页面
        self.load_wait()
        return ContactManagePage(self.driver)


class AddMemberPage(WeWorkApp):
    """
    添加成员界面（通讯录子界面）
    """
    _MANUAL_INPUT_BTN = AppiumBy.ID, 'com.tencent.wework:id/eas'

    def goto_manual_input_page(self):
        print("点击【手动输入添加】按钮")
        self.driver.find_and_click(*self._MANUAL_INPUT_BTN)

        # 跳转手动添加页面
        self.load_wait()
        return ManualInputPage(self.driver)


class ManualInputPage(WeWorkApp):
    """
    手动添加界面（添加成员子界面）
    """
    # 完整输入/快速输入
    _INPUT_TYPE_SWITCH_BTN = AppiumBy.ID, 'com.tencent.wework:id/mht'
    # 姓名输入框
    _NAME_INPUT = AppiumBy.XPATH, '//android.widget.EditText[@resource-id="com.tencent.wework:id/c__" and @text="必填"]'
    # 手机号输入框
    _PHONE_INPUT = AppiumBy.ID, 'com.tencent.wework:id/j6_'
    # 取消发送邀请通知
    _SEND_MESSAGE_BOX = AppiumBy.ID, 'com.tencent.wework:id/int'
    # 保存按钮
    _SAVE_BTN = AppiumBy.ID, 'com.tencent.wework:id/b60'
    # Error提示信息
    _ERROR_MESSAGE = AppiumBy.XPATH, '//android.widget.Toast'
    # Error对话框信息
    _ERROR_DIALOG_TEXT = AppiumBy.ID, 'com.tencent.wework:id/cy9'

    def input_member_info(self, name, phone):
        """
        快速输入添加成员
        return: 点击提交表单后的界面
        """
        ele_input_type = self.driver.find_element(*self._INPUT_TYPE_SWITCH_BTN)
        if ele_input_type.text == '快速输入':
            ele_input_type.click()

        print(f"输入姓名：{name}")
        self.driver.find_and_send_keys(*self._NAME_INPUT, name)
        err_msg = self.driver.get_err_msg(*self._ERROR_MESSAGE)
        if err_msg:
            print(f"提示错误：{err_msg}")
            return err_msg

        print(f"输入手机号：{phone}")
        self.driver.find_and_send_keys(*self._PHONE_INPUT, phone)

        print("点击【取消发送邀请通知】")
        self.driver.find_and_click(*self._SEND_MESSAGE_BOX)

        print("点击【保存】")
        self.driver.find_and_click(*self._SAVE_BTN)

        self.load_wait()
        err_msg = self.driver.get_err_msg(*self._ERROR_DIALOG_TEXT)
        if err_msg:
            print(f"提示错误弹窗：{err_msg}")
            return err_msg
        return AddMemberPage(self.driver)


class ContactManagePage(WeWorkApp):
    """
    联系人管理界面（联系人子界面）
    """
    _CONTACT_XPATH_FORMATE = '//android.widget.TextView[@resource-id="com.tencent.wework:id/gje" and @text="{}"]'

    def get_contact_xpath(self, name: str):
        return self._CONTACT_XPATH_FORMATE.format(name)

    def select_and_editor_contact(self, name: str):
        """
        选择联系人并编辑
        """
        print(f"点击联系人：{name}")
        self.driver.vertical_swipe_find_and_click(AppiumBy.XPATH, self.get_contact_xpath(name))

        print(f"跳转{name}编辑页面")
        self.load_wait()
        return ContactEditorPage(self.driver)


class ContactEditorPage(WeWorkApp):
    """
    联系人编辑界面（联系人管理子界面）
    """
    _NAME_INPUT = AppiumBy.XPATH, '(//android.widget.EditText[@resource-id="com.tencent.wework:id/c__"])[1]'
    _SEX_SELECTOR = AppiumBy.XPATH, '(//android.widget.RelativeLayout[@resource-id="com.tencent.wework:id/cab"])[1]'
    _MAN_OPTION_BTN = AppiumBy.XPATH, '(//android.widget.RelativeLayout[@resource-id="com.tencent.wework:id/cmc"])[1]'
    _SAVE_BTN = AppiumBy.ID, 'com.tencent.wework:id/mi4'
    _DEL_MEMBER_BTN = AppiumBy.ID, 'com.tencent.wework:id/hks'
    _CONFIRE_DEL_BTN = AppiumBy.ID, 'com.tencent.wework:id/d6c'
    _ERROR_MESSAGE = AppiumBy.XPATH, '//android.widget.Toast'
    _ERROR_DIALOG_TEXT = AppiumBy.ID, 'com.tencent.wework:id/cy9'

    def editor_and_save(self, name):
        print(f"修改【姓名】：{name}")
        self.driver.find_and_reset(*self._NAME_INPUT, name)
        err_msg = self.driver.get_err_msg(*self._ERROR_MESSAGE)
        if err_msg:
            print(f"提示错误：{err_msg}")
            return err_msg

        # 须重新点一下性别 保存按钮才能点
        self.driver.find_and_click(*self._SEX_SELECTOR)
        self.load_wait()
        self.driver.find_and_click(*self._MAN_OPTION_BTN)
        self.load_wait()

        print("点击【保存】按钮")
        self.driver.find_and_click(*self._SAVE_BTN)

        self.load_wait()
        err_msg = self.driver.get_err_msg(*self._ERROR_DIALOG_TEXT)
        if err_msg:
            print(f"提示错误弹窗：{err_msg}")
            return err_msg
        return ContactManagePage(self.driver)

    def del_member(self):
        print("滑动寻找并点击【删除成员】按钮")
        self.driver.vertical_swipe_find_and_click(*self._DEL_MEMBER_BTN)
        self.load_wait()
        err_msg = self.driver.get_err_msg(*self._ERROR_DIALOG_TEXT)
        if err_msg:
            print(f"提示错误弹窗：{err_msg}")
            return err_msg

        print("点击弹出页面的【删除】按钮")
        self.driver.find_and_click(*self._CONFIRE_DEL_BTN)

        self.load_wait()
        return ContactManagePage(self.driver)
